package dxn.nl.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dxn.boot.common.entity.CsBaseCodeNameDto;
import dxn.boot.common.entity.CsBasePageQuery;
import dxn.boot.common.entity.CsBasePageResult;
import dxn.nl.file.biz.dao.AppModeOrgBindDao;
import dxn.nl.file.biz.dao.AppModelDao;
import dxn.nl.file.biz.po.AppModeOrgBindPo;
import dxn.nl.file.biz.po.AppModelPo;
import dxn.nl.file.biz.vo.CsExportModelUserBindVo;
import dxn.nl.file.service.AppModeOrgBindService;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class AppModeOrgBindServiceImpl extends ServiceImpl<AppModeOrgBindDao, AppModeOrgBindPo> implements AppModeOrgBindService {
    @Resource
    private AppModelDao appModelDao;

    @Override
    public String modeUnBindOrg(CsExportModelUserBindVo vo) {

        return null;
    }

    @Override
    public String modeBindOrg(CsExportModelUserBindVo vo) {

        return null;
    }

    @Override
    public CsBasePageResult<CsBaseCodeNameDto> pageModeUnBindOrg(CsBasePageQuery<CsExportModelUserBindVo> pageQuery) {

        return null;
    }

    @Override
    public CsBasePageResult<CsBaseCodeNameDto> pageModeBindOrg(CsBasePageQuery<CsExportModelUserBindVo> pageQuery) {
        CsExportModelUserBindVo vo = pageQuery.getQuery();
        LambdaQueryWrapper<AppModeOrgBindPo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AppModeOrgBindPo::getModeCode, vo.getModeCode())
                .eq(AppModeOrgBindPo::getExportTypeCode, vo.getExportTypeCode());
        if(!CollectionUtils.isEmpty(vo.getMemberList()))
        {
            lambdaQueryWrapper.in(AppModeOrgBindPo::getCode,vo.getMemberList());
        }

        lambdaQueryWrapper.orderByAsc(AppModeOrgBindPo::getCode);

        IPage<AppModeOrgBindPo> page= new Page();
        page = this.baseMapper.selectPage(page,lambdaQueryWrapper);

        CsBasePageResult<CsBaseCodeNameDto> pageResult = new CsBasePageResult<>();
        pageResult.setTotal(page.getTotal());

        List<CsBaseCodeNameDto> resultList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(page.getRecords()))
        {
            for(AppModeOrgBindPo binding : page.getRecords())
            {
                CsBaseCodeNameDto dto = new CsBaseCodeNameDto();
                dto.setCode(binding.getCode());
                dto.setName(binding.getName());
                resultList.add(dto);
            }
        }

        pageResult.setRows(resultList);
        return pageResult;
    }
}
